package com.snts.synchronization.protocol.adapter;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import nu.xom.Attribute;
import nu.xom.Element;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.snts.synchronization.core.Change;
import com.snts.synchronization.core.ChangeColumn;
import com.snts.synchronization.core.SyncRequest;
import com.snts.synchronization.core.SyncResponse;
import com.snts.synchronization.core.Synchronization;
import com.snts.synchronization.protocol.ProtocolException;

public class SyncResponsePA extends SyncPackagePA<SyncResponse> {
	
	private static final Log log = LogFactory.getLog(SyncResponsePA.class);
	
	private SyncResponse syncResponse;
	private DefaultHandler syncPackageParser;
	private Synchronization synchronization;

	public void setSynchronization(Synchronization synchronization) {
		this.synchronization = synchronization;
	}

	@Override
	protected SyncResponse popMessage() {
		return syncResponse;
	}

	@Override
	protected Element getMessageXOM(SyncResponse message) {
		Element root = new Element("sync-response");
		addItems(message, root);
		return root;
	}
	
	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		super.characters(ch, start, length);
		if(syncPackageParser != null){
			syncPackageParser.characters(ch, start, length);
		}
	}

	@Override
	protected void doEndElement(String uri, String localName, String qName, String textValue) throws SAXException {
		if(SYNC_RESPONSE.equals(qName)){
			syncPackageParser = null;
		} else {
			syncPackageParser.endElement(uri, localName, qName);
		}
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		super.startElement(uri, localName, qName, attributes);
		if(syncPackageParser == null && !SYNC_RESPONSE.equals(qName)){
			String errorMessage = "invalid start element for Synchronization Response \"" + qName + "\"";
			log.fatal(errorMessage);
			throw new SyncParseException(errorMessage);
		} if(SYNC_RESPONSE.equals(qName)){
			syncResponse = new SyncResponse();
			syncPackageParser = getSyncPackageParser(syncResponse, synchronization);
		} else {
			syncPackageParser.startElement(uri, localName, qName, attributes);
		}
	}

	public static void main(String[] args) throws Exception {

		String str = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
				+ "<sync-response session-id=\"fb274f68-cfdc-11e1-9e53-d3cb457f6e55\">"
				+ "   <update schema=\"public\" table=\"table1\" row-id=\"9b274f69-cfdc-11e1-9e53-d3cb457f6e55\">"
				+ "			<column name=\"col1\">"
				+ "				The value for column"
				+ "         </column>"
				+ "   </update>"
				+ "   <update schema=\"public\" table=\"table1\" row-id=\"9b274f68-cfdc-11e1-9e53-d3cb457f6e55\">"
				+ "			<column name=\"col1\">"
				+ "				The value for column1"
				+ "         </column>"
				+ "			<column name=\"col2\">"
				+ "              1"
				+ "         </column>"
				+ "   </update>"
				+ "</sync-response>";

		Connection conn = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/Restaurant", "postgres", "123");
		Synchronization synchronization = new Synchronization();
		synchronization.load(conn.getMetaData());

		SyncResponsePA syncResponsePA = new SyncResponsePA();
		syncResponsePA.setSynchronization(synchronization);
		SyncResponse syncResponse = syncResponsePA.parseMessage(new ByteArrayInputStream(str.getBytes("UTF-8")));
		
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		syncResponsePA.serializeMessage(syncResponse, baos);
		
		System.out.println(new String(baos.toByteArray()));

	}

}
